class Solution {//leetcode230——二叉搜索树中第k小的元素
public:
    void dfs(TreeNode* root){
        if(root==nullptr) return;
        if(cnt==0) return;
        dfs(root->left);
        cnt--;
        if(cnt==0) key=root->val; 
        dfs(root->right);
    }
    int kthSmallest(TreeNode* root, int k) {
        cnt=k;
        dfs(root);
        return key;
    }
private:
    int key=0;
    int cnt;
};